package com.tuyou.cloud.ribbon.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@Slf4j
@RestController
public class ConsumerController {

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    LoadBalancerClient loadBalancerClient;

    @RequestMapping(value = "/ribbon-consumer", method = RequestMethod.GET)
    public String helloConsumer() {
        ServiceInstance instance = loadBalancerClient.choose("HELLO-SERVICE");
        logger.info("serviceId:{},host:{},port:{}", instance.getServiceId(), instance.getHost(), instance.getPort());
        return restTemplate.getForEntity("http://HELLO-SERVICE/hello", String.class).getBody();
    }

    @RequestMapping(value = "/param", method = RequestMethod.GET)
    public String paramTest(){
        return restTemplate.getForObject("http://HELLO-SERVICE/param?name={1}&age={2}", String.class, "tuyou", 25);
    }
}
